#include "viewer-editable.h"

G_DEFINE_INTERFACE (ViewerEditable, viewer_editable, G_TYPE_OBJECT)

// interface singal
enum {
    SAVED,
    LAST_SIGNAL
};
static guint editable_signals[LAST_SIGNAL] = {0};

static void
viewer_editable_default_init (ViewerEditableInterface *iface)
{
    g_print("calling %s\n", __func__);
    /* add properties and signals to the interface here */
    g_object_interface_install_property (iface,
        g_param_spec_double ("autosave-frequency",
                             "Autosave frequency",
                             "Frequency (in per-seconds) to autosave backups of the editable content at. "
                             "Or zero to disable autosaves.",
                             0.0,  /* minimum */
                             G_MAXDOUBLE,  /* maximum */
                             0.0,  /* default */
                             G_PARAM_READWRITE));
    editable_signals[SAVED] = 
        g_signal_new("saved", //singal
                     G_TYPE_FROM_INTERFACE(iface), //GType
                     0, // signal flags
                     0, //interface offset of per-handler
                     NULL, //signal accumulator
                     NULL, // accumulator data
                     NULL, // C marshller
                     G_TYPE_NONE, /* return type */
                     0 /* extra parameters */); 
}

void
viewer_editable_save (ViewerEditable  *self,
                      const gchar *name)
{
  ViewerEditableInterface *iface;

  g_return_if_fail (VIEWER_IS_EDITABLE (self));
  //g_return_if_fail (error == NULL || *error == NULL);

  iface = VIEWER_EDITABLE_GET_IFACE (self);
  g_return_if_fail (iface->save != NULL);
  iface->save (self, name);
  g_signal_emit(self, editable_signals[SAVED], 0);
}

void
viewer_editable_undo (ViewerEditable *self,
                      guint           n_steps)
{
  ViewerEditableInterface *iface;

  g_return_if_fail (VIEWER_IS_EDITABLE (self));

  iface = VIEWER_EDITABLE_GET_IFACE (self);
  g_return_if_fail (iface->undo != NULL);
  iface->undo (self, n_steps);
}

void
viewer_editable_redo (ViewerEditable *self,
                      guint           n_steps)
{
  ViewerEditableInterface *iface;

  g_return_if_fail (VIEWER_IS_EDITABLE (self));

  iface = VIEWER_EDITABLE_GET_IFACE (self);
  g_return_if_fail (iface->redo != NULL);
  iface->redo (self, n_steps);
}
